package adfc2.render3d.mesh;

import adfc2.render3d.TextureReference;

import javax.media.opengl.GL;

/**
 * Примитив --- параллелепипед
 */
public class Box extends Primitive {

    /**
     * @param xyz     координаты левого ближнего нижнего угла
     * @param size    размеры по xyz
     * @param texture текстура (или null)
     */
    public Box(double[] color, double[] xyz, double[] size, TextureReference texture) {
        super(GL.GL_QUADS, color, mkBoxVertices(xyz, size), BOX_TEX_COORD, BOX_NORMALS, texture);
    }

    static double[] UNIBOX = {
            1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0,
            1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0,
            0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0,
            1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0,
            0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0,
            1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1,
    };
    static double[] BOX_NORMALS = {
            0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1,
            0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0,
            -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0,
            1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
            0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
            0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1
    };
    static double[] BOX_TEX_COORD = {
            0, 0, 1, 0, 1, 1, 0, 1,
            0, 0, 1, 0, 1, 1, 0, 1,
            0, 0, 1, 0, 1, 1, 0, 1,
            0, 0, 1, 0, 1, 1, 0, 1,
            0, 0, 1, 0, 1, 1, 0, 1,
            0, 0, 1, 0, 1, 1, 0, 1,
    };

    private static double[] mkBoxVertices(double[] xyz, double[] size) {
        double[] dd = new double[UNIBOX.length];
        for (int i = 0; i < dd.length; ) {
            dd[i] = UNIBOX[i] * size[0] + xyz[0];
            i++;
            dd[i] = UNIBOX[i] * size[1] + xyz[1];
            i++;
            dd[i] = UNIBOX[i] * size[2] + xyz[2];
            i++;
        }
        return dd;
    }
}
